package com.tnl.app.dao;

import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.tnl.app.entity.Evaluate;
import com.tnl.app.entity.WxAppUser;
import com.tnl.base.BaseDAO;
import com.tnl.utils.DateUtil;

@Repository
public class WxAppUserDAO {
	Logger logger = LoggerFactory.getLogger(WxAppUserDAO.class);

	private static final String SQL_SAVE = "insert into wxapp_user(username,password,gender,birthday,"
			+ "registerTime,lastLoginTime,userlevelId,nickname,mobile,avatar,openid,language,city,province,country,unionid) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	private static final String SQL_SAVE_USERINFO = "insert into wxapp_user(username,gender,registerTime,nickname,avatar,openid,unionid,language,city,province,country)"
			+ " values(?,?,?,?,?,?,?,?,?,?,?)";
	
	private static final String SQL_DELETE = "delete from wxapp_user where openid=?";
	private static final String SQL_GET_LIST = "select * from wxapp_user where openid in (";
	private static final String SQL_GET = "select * from wxapp_user where openid = ?";
	private static final String SQL_GET_COUNT_OPENID="select count(*) from wxapp_user where openid=?";
	//username,gender,registerTime,nickname,avatar,openid,unionid,language,city,province,country
	private static final String SQL_UPDATE = "update wxapp_user set username=?,gender=?,"
			+ " nickname=?,avatar=?,unionid=?,language=?,city=?,province=?,country=? where openid =?";
	private static final String SQL_BIND_PHONE="insert into wxapp_user(mobile,openid) values(?,?)";
	private static final String SQL_UPDATE_MOBILE="update wxapp_user set mobile=?,purePhoneNumber=?,countryCode=? where openid =?";
	
	public int save(WxAppUser... users) {
		if (users != null) {
			if (users.length == 1) {
				WxAppUser user = users[0];
				return BaseDAO.execute(SQL_SAVE, user.getUsername(),
						user.getPassword(), user.getGender(),
						user.getBirthday(), user.getRegisterTime(),
						user.getLastLoginTime(),
						user.getUserlevelId(), user.getNickname(),
						user.getMobile(),
						user.getAvatar(), user.getOpenid(),user.getLanguage(),
						user.getCity(),user.getProvince(),user.getCountry(),user.getUnionid());

			} else {
				Object[][] params = new Object[users.length][16];
				int i = 0;
				for (WxAppUser user : users) {
					params[i][0] = user.getUsername();
					params[i][1] = user.getPassword();
					params[i][2] = user.getGender();
					params[i][3] = user.getBirthday();
					params[i][4] = user.getRegisterTime();
					params[i][5] = user.getLastLoginTime();
					params[i][6] = user.getUserlevelId();
					params[i][7] = user.getNickname();
					params[i][8] = user.getMobile();
					params[i][9] = user.getAvatar();
					params[i][10] = user.getOpenid();
					params[i][11] = user.getLanguage();
					params[i][12] = user.getCity();
					params[i][13] = user.getProvince();
					params[i][14] = user.getCountry();
					params[i][15] = user.getUnionid();
					i++;
				}
				int[] rows = BaseDAO.batch(SQL_SAVE, params);
				if (rows != null && rows.length > 0) {
					int total = 0;
					for (int r : rows) {
						total += r;
					}
					return total;
				} else {
					return -1; // 数据库访问异常�?
				}
			}
		} else {
			return 0;
		}
	}

	public int delete(String openId) {
		if (StringUtils.isEmpty(openId)) {
			return -1;
		}
		return BaseDAO.execute(SQL_DELETE, openId);

	}

	public List<WxAppUser> get(String... userIds) {

		if (userIds != null) {
			StringBuilder sql = new StringBuilder(SQL_GET_LIST);
			boolean start = false;
			for (String id : userIds) {
				if (start) {
					sql.append(",").append(id);
				} else {
					start = true;
					sql.append(id);
				}
			}
			sql.append(")");
			return BaseDAO.queryForList(WxAppUser.class, sql.toString());

		} else {
			return Collections.emptyList();
		}
	}

	public int update(WxAppUser user) {
		return BaseDAO.execute(SQL_UPDATE, user.getNickname(),
				user.getGender(),
				user.getNickname(), user.getAvatar(),
				user.getUnionid(),
				user.getLanguage(),
				user.getCity(),user.getProvince(),user.getCountry(),user.getOpenid());
	}


	public int saveUserInfo(WxAppUser user) {
		if (user == null) {
			return -1;
		}
		Long id = BaseDAO.save(SQL_SAVE_USERINFO,user.getNickname(),user.getGender(),
				DateUtil.currentTime(), user.getNickname(), user.getAvatar(),
				user.getOpenid(), user.getUnionid(),user.getLanguage(),
				user.getCity(), user.getProvince(),
				user.getCountry());
		if (id != null) {
			return id.intValue();
		}
		return -1;
	}
	public WxAppUser getByOpenId(String openid) {
		return BaseDAO.queryForObject(WxAppUser.class, SQL_GET, openid);
	}

	public Long getOpenId(String openid){
		return (Long)BaseDAO.queryOneRowOneCol(SQL_GET_COUNT_OPENID, openid);
	}
	public int unSubscribe(String openId) {
		int result = -1;
		result = delete(openId);
		return result;
	}

	public int savecode(String phone,String openid){
		int result=BaseDAO.execute(SQL_BIND_PHONE, phone,openid);
	    return result;
	}
	
	public int updateMobile(String mobile,String purePhoneNumber,String countryCode,String openId){
		int result = BaseDAO.execute(SQL_UPDATE_MOBILE,mobile,purePhoneNumber,countryCode,openId);
		return result;
	}

	

}
